package com.unlcn.ils.erpInterfaces.api.controller;

import com.unlcn.ils.erpInterfaces.api.vo.RestVO;
import com.unlcn.ils.erpInterfaces.api.vo.WmsOrderVO;
import com.unlcn.ils.erpInterfaces.dao.model.bo.WmsOrderLogBo;
import com.unlcn.ils.erpInterfaces.service.buss.IWmsOrderLogService;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/order", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/order", description = "入库订单接口",tags={"导入入库订单接口"})
public class WmsToOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WmsToOrderController.class);

    @Autowired
    private IWmsOrderLogService iWmsOrderLogService;


    @PostMapping(value = "/importOrder")
    @ApiOperation(value = " wms 导入入库订单", notes = " wms 导入入库订单", produces = MediaType.APPLICATION_JSON_VALUE,response = RestVO.class)
    public RestVO<String> importOrder(@RequestBody WmsOrderVO orderVO) throws Exception {
        LOGGER.info("WmsToOrderController.importOrder params : {}", orderVO);
        RestVO<String> result = new RestVO<>(0, null, "success");

        WmsOrderLogBo wmsOrderLogBo = null;

        try {
            wmsOrderLogBo = new WmsOrderLogBo();
            BeanUtils.copyProperties(orderVO,wmsOrderLogBo);
            result.setData(iWmsOrderLogService.saveOrderByWms(wmsOrderLogBo));
            result.setSuccess(true);
        }  catch (Exception e) {
            LOGGER.error("WmsToOrderController.importOrder ERROR Message : {} ", e);
            result.setCode(-1);
            result.setMessage(e.getMessage());
            result.setSuccess(false);
            iWmsOrderLogService.saveOrderException(wmsOrderLogBo, e.getMessage());
        }

        return result;
    }
}
